package com.bite.messageQueue.mqServer.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * 表示交换机
 */
public class Exchange {
    //用name来表示交换机的身份标识
    private String name;
    //交换机类型
    private ExchangeType exchangeType = ExchangeType.DIRECT;
    //表示该交换机是否要持久化
    private boolean durable = false;
    //如果当前交换机没人使用了就会自动删除
    private boolean autoDelete = false;
    //表示交换机的一些扩展参数选项
    private Map<String, Object> arguments = new HashMap<>();

    public Exchange() {
    }

    public Exchange(String name, ExchangeType exchangeType, boolean durable, boolean autoDelete, Map<String, Object> arguments) {
        this.name = name;
        this.exchangeType = exchangeType;
        this.durable = durable;
        this.autoDelete = autoDelete;
        this.arguments = arguments;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return exchangeType
     */
    public ExchangeType getExchangeType() {
        return exchangeType;
    }

    /**
     * 设置
     * @param exchangeType
     */
    public void setExchangeType(ExchangeType exchangeType) {
        this.exchangeType = exchangeType;
    }

    /**
     * 获取
     * @return durable
     */
    public boolean isDurable() {
        return durable;
    }

    /**
     * 设置
     * @param durable
     */
    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    /**
     * 获取
     * @return autoDelete
     */
    public boolean isAutoDelete() {
        return autoDelete;
    }

    /**
     * 设置
     * @param autoDelete
     */
    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    /**
     * 获取
     * @return arguments
     */
    public String getArguments(){
        //将arguments的Map格式转为JSON格式
        ObjectMapper objectMapper = new ObjectMapper();
        try{
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //发生异常返回空字符串
        return "{}";
    }

    /**
     * 从数据库中读数据之后，构造交换机对象
     * @param argumentsJSON
     */
    public void setArguments(String argumentsJSON) {
        //将JSON格式转为Map格式
        ObjectMapper objectMapper = new ObjectMapper();
        try{
            this.arguments = objectMapper.readValue(argumentsJSON, new TypeReference<HashMap<String, Object>>() {});
        }catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    // 根据key获取参数
    public Object getArguments(String key) {
        // 从arguments中获取key对应的参数
        return arguments.get(key);
    }

    public void setArguments(String key, Object value) {
        arguments.put(key, value);
    }

    public void setArguments(Map<String,Object> arguments) {
        this.arguments = arguments;
    }

    @Override
    public String toString() {
        return "Exchange{name = " + name + ", exchangeType = " + exchangeType + ", durable = " + durable + ", autoDelete = " + autoDelete + ", arguments = " + arguments + "}";
    }
}
